﻿using System;
using System.Management;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Convert a Disk ManagementObject to Disk Class
    /// </summary>
    public static class DiskTranslate
    {
        /// <summary>
        /// Convert Disk ManagementObject to Disk Class
        /// </summary>
        /// <param name="clsDisk">Disk class</param>
        /// <param name="queryObj">ManagementObject Group</param>
        /// <returns>Disk class with properties converted</returns>
        public static DiskProperty Convert(this DiskProperty clsDisk, ManagementObject queryObj)
        {
            clsDisk.Caption = queryObj.PropertyValue<String>("Caption");
            clsDisk.Characteristics = queryObj.PropertyEnum<UInt32, CharacteristicsResource>("Characteristics", CharacteristicsResource.Unknown);
            clsDisk.Description = queryObj.PropertyValue<String>("Description");
            clsDisk.Flags = queryObj.PropertyValue<UInt32>("Flags");
            clsDisk.Id = queryObj.PropertyValue<String>("ID");
            clsDisk.InstallDate = queryObj.PropertyValue<DateTime?>("InstallDate");
            clsDisk.Name = queryObj.PropertyValue<String>("Name");
            clsDisk.ScsiBus = queryObj.PropertyValue<UInt32>("ScsiBus");
            clsDisk.ScsiLun = queryObj.PropertyValue<UInt32>("ScsiLUN");
            clsDisk.ScsiPort = queryObj.PropertyValue<UInt32>("ScsiPort");
            clsDisk.ScsiTargetId = queryObj.PropertyValue<UInt32>("ScsiTargetID");
            clsDisk.Signature = queryObj.PropertyValue<UInt32>("Signature");
            clsDisk.Status = queryObj.PropertyEnum<string, StatusResource>("Status", StatusResource.Unknown);
            return clsDisk;
        }

        ///<summary>
        /// Convert Disk class to ManagementObject and Save on Cluster
        ///</summary>
        ///<param name="clsDisk">Disk class to converter</param>
        public static void SaveManagementObject(this DiskProperty clsDisk)
        {
            clsDisk.Instance["Characteristics"] = (UInt32)clsDisk.Characteristics;
            clsDisk.Instance["Flags"] = clsDisk.Flags;
            clsDisk.Instance.Put();
        }
    }
}
